import java.util.Random;

class Solution {
    public int findKthLargest(int[] nums, int k) {
        qsort(nums, 0, nums.length - 1);
        return nums[k - 1];
    }
    public void qsort(int[] nums, int l, int r) {
        if (l >= r) return;
        int x = nums[new Random().nextInt(r - l + 1) + l];
        int left = l - 1, right = r + 1, i = l;
        while (i < right) {
            if (nums[i] > x) {
                swap(nums, ++left, i++);
            } else if (nums[i] == x) {
                i++;
            } else {
                swap(nums, --right, i);
            }
        }
        qsort(nums, l, left);
        qsort(nums, right, r);
    }
    public void swap(int[] nums, int a, int b) {
        int t = nums[a];
        nums[a] = nums[b];
        nums[b] = t;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.findKthLargest(new int[]{3, 2, 1, 5, 6, 4}, 2));
    }
}